$\textbf{Part 2:}$

In [1]:
from Image import my_image,Show,segmentation
import numpy as np
import cv2
from skimage import color
import matplotlib.pyplot as plt

$ \\ \textbf{Edge detection:} $

a) Sobel :
-Detects edges are where the gradient magnitude is high.This makes the Sobel edge detector more sensitive to diagonal edge than horizontal and vertical edges.
-The operator uses two kernels which are convolved with the original image to calculate approximations of the derivatives (one for horizontal changes, and one for vertical). If we define A as the source image, and Gx and Gy are two images which at each point contain the vertical and horizontal derivative approximations.
- At each point in the image, the resulting gradient approximations can be combined to give the gradient magnitude.
-above process is in Image.py

In [2]:
img1=my_image.readimage('images/PGN2.jpg')
img2=my_image.readimage('images/edgedetection/test1.png')
img3=my_image.readimage('images/test5.png')
img4=my_image.readimage('images/edgedetection/test2.png')
img5=my_image.readimage('images/edgedetection/test3.png')


img1_,edge1=my_image.edge_detection(img1,mod='sobel',blur_sigma=0.2)
img2_,edge2=my_image.edge_detection(img2,mod='sobel',blur_sigma=0.2)
img3_,edge3=my_image.edge_detection(img3,mod='sobel',blur_sigma=0.2)
img4_,edge4=my_image.edge_detection(img4,mod='sobel',blur_sigma=0.2)
img5_,edge5=my_image.edge_detection(img5,mod='sobel',blur_sigma=0.2)



Show.compareim(img1,edge1,'','',size=2)
Show.compareim(img2,edge2,'','',size=1)
Show.compareim(img3,edge3,'','',size=1.5)
Show.compareim(img4,edge4,'','',size=1)
Show.compareim(img5,edge5,'','',size=1)

$\star$after edge detection with sobel; i use thresholding. So it takes a little more time to compute.


b) Laplace :
-Unlike the Sobel edge detector, the Laplacian edge detector uses only one kernel. It calculates second order derivatives in a single pass so it is computationally faster to calculate (only one kernel vs two kernels)
-because we're working with second order derivatives, the laplacian edge detector is extremely sensitive to noise.so we reduce noise first by median filter and second with Gauss filter

In [3]:
img1=my_image.readimage('images/PGN2.jpg')
img2=my_image.readimage('images/edgedetection/test1.png')
img3=my_image.readimage('images/test5.png')
img4=my_image.readimage('images/edgedetection/test2.png')
img5=my_image.readimage('images/edgedetection/test3.png')


Img1_,Edge1=my_image.edge_detection(img1,mod='laplace',blur_sigma=0.2,floor=25)
Img2_,Edge2=my_image.edge_detection(img2,mod='laplace',blur_sigma=5,floor=25)
Img3_,Edge3=my_image.edge_detection(img3,mod='laplace',blur_sigma=1.4,floor=25)
Img4_,Edge4=my_image.edge_detection(img4,mod='laplace',blur_sigma=1,floor=25)
Img5_,Edge5=my_image.edge_detection(img5,mod='laplace',blur_sigma=1,floor=25)



Show.compareim(img1,Edge1,'','',size=2)
Show.compareim(img2,Edge2,'','',size=1.2)
Show.compareim(img3,Edge3,'','',size=1.5)
Show.compareim(img4,Edge4,'','',size=1.2)
Show.compareim(img5,Edge5,'','',size=1.2)


as we can see below laplace method is more sensitive to noise than sobel method:

In [4]:
Show.compareim(edge5,Edge5,'Sobel method','Laplace method (LOG)',1)

$ \\ \textbf{2.Hough transforms:} $

a)Line detection :
-first i do edge detection after importing the image
-second i threshold it
-third i do line detection with HoughLinesP

In [5]:
img1=my_image.readimage('images/edgedetection/test1.png')
img2=my_image.readimage('images/test5.png')
img3=my_image.readimage('images/test8.jpg')
img4=my_image.readimage('images/test9.png')

img1,edge1,line_detect1=my_image.line_detection(img1)
img2,edge2,line_detect2=my_image.line_detection(img2)
img3,edge3,line_detect3=my_image.line_detection(img3)
img4,edge4,line_detect4=my_image.line_detection(img4)

Show.compareim(img1,edge1,'original','edge',1.2,1,line_detect1,'line detection')
Show.compareim(img2,edge2,'original','edge',2,1,line_detect2,'line detection')
Show.compareim(img3,edge3,'original','edge',2,1,line_detect3,'line detection')
Show.compareim(img4,edge4,'original','edge',2,1,line_detect4,'line detection')

b)Circle detection :

In [6]:
origin1=my_image.readimage('images/t2.jpg')
origin2=my_image.readimage('images/t3.png')
origin3=my_image.readimage('images/w4.png')
origin4=my_image.readimage('images/ex3.png')

cimg1=my_image.circle_detection(origin1)
cimg2=my_image.circle_detection(origin2,5,7,14)
cimg3=my_image.circle_detection(origin3,5,3,11)
cimg4=my_image.circle_detection(origin4,3,7)

Show.compareim(origin1,cimg1,'original','circle detection',1.2)
Show.compareim(origin2,cimg2,'original','circle detection',1.2)
Show.compareim(origin3,cimg3,'original','circle detection',1.2)
Show.compareim(origin4,cimg4,'original','circle detection',1.2)
In [ ]:
 
In [7]:
img1=my_image.readimage('images/mm.jpg')
line=my_image.line_detection(img1)

Show.show_me(line[2])

$ \\ \textbf{3. Video processing} $

$\textbf{I. ColorSegmentator(image, min_color, max_color)}$

In [8]:
pen = my_image.readimage('images/color2.jpg')
pen = cv2.medianBlur(pen, ksize=11)

result1_redpink=segmentation.ColorSegmentator(pen,120,179)
result1_blue=segmentation.ColorSegmentator(pen,85,120)
result1_yellow=segmentation.ColorSegmentator(pen,22,35)
result1_orange=segmentation.ColorSegmentator(pen,8,17)
result1_green=segmentation.ColorSegmentator(pen,36,80)

Show.compareim(result1_orange,pen,'orange detection','original')
Show.compareim(result1_yellow,result1_redpink,'yellow detection', 'red  & pink detection')
Show.compareim(result1_blue,result1_green,'blue detection','green detection')
In [9]:
nemo = my_image.readimage('images/color1.jpg')

result1_redpink=segmentation.ColorSegmentator(nemo,119,179)
result1_blue=segmentation.ColorSegmentator(nemo,85,120)
result1_yellow=segmentation.ColorSegmentator(nemo,22,35)
result1_orange=segmentation.ColorSegmentator(nemo,0,13)
result1_green=segmentation.ColorSegmentator(nemo,36,80)

Show.compareim(result1_orange,nemo,'orange detection','original')
Show.compareim(result1_redpink,result1_yellow,'red detection','yellow detection')
Show.compareim(result1_blue,result1_green,'blue detection','green detection')

$\textbf{II. LinesDetector(image, minlenght)}$

In [10]:
img1=my_image.readimage('images/edgedetection/test1.png')
img2=my_image.readimage('images/test5.png')
img3=my_image.readimage('images/test8.jpg')
img4=my_image.readimage('images/t3.png')



line1=segmentation.LinesDetector(img1,minlenght=30,r=0,g=255,b=0)
line2=segmentation.LinesDetector(img2,minlenght=30,r=0,g=0,b=255)
line3=segmentation.LinesDetector(img3,minlenght=30)
line4=segmentation.LinesDetector(img4,minlenght=10)

Show.compareim(line1,line2)
Show.compareim(line3,line4)

$\textbf{III. PolygonDetector(image, maxside)}$

In [11]:
img1=cv2.imread('images/edgedetection/test1.png')
img2=cv2.imread('images/t4.jpg')
img4=cv2.imread('images/t3.png')

s1=segmentation.PolygonDetector(img1,6,r=0,g=255,b=0)
s2=segmentation.PolygonDetector(img2,7)
s4=segmentation.PolygonDetector(img4,7)

s1 = cv2.cvtColor(s1, cv2.COLOR_BGR2RGB)
s2 = cv2.cvtColor(s2, cv2.COLOR_BGR2RGB)
s4 = cv2.cvtColor(s4, cv2.COLOR_BGR2RGB)
Show.compareim(s1,s2,'max=6','max7',1)
Show.show_me(s4,'max =7',scale=0.9)

$\textbf{IV:color segmentation with WEBCAM .}$

$ \text{exit with Esc}$

In [12]:
segmentation.camera_color()
In [13]:
segmentation.camera_Line(minlenght=30)
In [14]:
segmentation.camera_Polygon(maxside=30)

$\textbf{X. Checkpoint}$